<template>
  <div class="app-container">
    <div style="    position: relative;">
      <el-button style="margin-bottom:10px;position: relative;right: -83%;" type="primary" size="mini" @click="toadd()">添加</el-button>
      <el-button style="margin-bottom:10px;;position: relative;right: -83%;" type="primary" size="mini" @click="requestData()">刷新</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading..."
      border
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80">
        <template slot-scope="{row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column label="关卡名">
        <template slot-scope="{row}">
          {{ row.training_level.title }}
        </template>
      </el-table-column>
        <el-table-column label="关卡标题">
        <template slot-scope="{row}">
          {{ row.head_name }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="{row}">
          {{ row.created_at }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160" align="center">
        <template slot-scope="{row}">
          {{ row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="edit(row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="del(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :page.sync="listQuery.page"
      :total="total"
      :limit.sync="listQuery.limit"
      @pagination="requestData"
    />
    <!-- add表弹框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisibleAdd" width="700px" :close-on-click-modal="false" :show-close="false">
      <el-form :model="formAdd">
        <el-form-item label="关卡名">
          <el-select v-model="formAdd.training_level_id" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关卡标题">
          <el-input v-model="formAdd.head_name" autocomplete="false"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- updata表弹框 -->
    <el-dialog title="修改" :visible.sync="dialogFormVisible" width="700px" :close-on-click-modal="false" :show-close="false">
      <el-form :model="formData">
        <el-form-item label="关卡名">
          <el-select v-model="formData.training_level_id" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关卡标题">
          <el-input v-model="formData.head_name" autocomplete="false"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updata()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import Pagination from '@/components/Pagination/index.vue'
import { _axiosGet, _axiosRemove, _axiosPut, _axiosPost } from '@/utils/axios-request'

@Component({
  name: 'ExportZip',
  components: {
    Pagination
  }
})
export default class extends Vue {
  private list: any[] = [];
  private options: any[] = [];
  private listLoading = false;
  private dialogFormVisible = false;
  private dialogFormVisibleAdd = false;
  private itemId = '';
  private formData = {
    head_name: '',
    training_level_id: ''
  };

  private formAdd = {
    head_name: '',
    training_level_id: ''
  };

  private listQuery = {
    page: 1,
    limit: 15,
    total: ''
  };

  private total = 0;

  created() {
    this.requestData()
  }

  // 加载chapter的数据
  private requestData() {
    this.listLoading = true
    _axiosGet('/api/admin/traininglevelheads', {
      limit: this.listQuery.limit,
      page: this.listQuery.page,
      include: 'trainingLevel'
    }).then(
      (response: any) => {
        if (response.code === 0) {
          this.list = response.data
          this.total = response.meta.total
        }
        this.listLoading = false
      }
    )
  }

  // 加载 programmingLanguages 数据
  private requestDataLanguage() {
    this.listLoading = true
    _axiosGet('/api/admin/traininglevels')
      .then(
        (response: any) => {
          if (response.code === 0) {
            this.options = response.data
          }
          this.listLoading = false
        }
      )
  }

  // 添加数据
  // 添加chapter
  private toadd() {
    this.dialogFormVisibleAdd = true
    this.requestDataLanguage()
  }

  private add() {
    // // 添加成功后关闭
    this.dialogFormVisibleAdd = false
    _axiosPost(
      '/api/admin/traininglevelheads', this.formAdd
    ).then(() => {
      this.formAdd.head_name = ''
      this.formAdd.training_level_id = ''
      this.requestData()
    })
  }

  // 编辑
  private edit(row: any) {
    this.itemId = row.id
    this.formData.head_name = row.head_name
    this.formData.training_level_id = row.training_level_id
    this.requestDataLanguage()
    this.dialogFormVisible = true
  }

  private updata() {
    // 提交成功后关闭
    this.dialogFormVisible = false
    _axiosPut(
      `/api/admin/traininglevelheads/${this.itemId}`,
      this.formData
    ).then(() => {
      this.formData.head_name = ''
      this.formData.training_level_id = ''
      this.requestData()
    })
  }

  // 删除
  private del(row: any) {
    this.$msgbox({
      title: '删除',
      message: '此操作将永久删除该文件, 是否继续?',
      type: 'warning',
      showClose: false,
      showCancelButton: true,
      closeOnClickModal: false
      // confirmButtonText: '确定',
      // cancelButtonText: '取消'
    }).then(() => {
      _axiosRemove(`/api/admin/traininglevelheads/${row.id}`).then(() => {
        this.requestData()
      })
    })
      .catch(() => {
        console.log('失败了')
      })
  }
}
</script>
